At rush hour, three
taxi buses arrived at the stop simultaneously, all following the same route,
and passengers immediately boarded them. The drivers noticed that the number of
people in the different buses varied and decided to transfer some passengers so
that each bus would have the same number of passengers. Determine the minimum
number of passengers that need to be transferred for this.
Input. Three integers,
not exceeding 100 – the number of passengers in the first, second, and
third busses, respectively.
Output. Print a single
number – the minimum number of passengers that need to be transferred. If this
is impossible, print IMPOSSIBLE.
Sample input |
Sample output |
1 2 3 |
1 |
mathematics
Let a, b, c represent the number of passengers in the first, second and
third minibuses respectively. For the number of passengers in minibuses to be
the equal after the transfer, the sum a + b + c must be divisible by 3.
Let d = (a + b + c)
/ 3 be the number
of passengers that
should be in each minibus after the transfer. Then it is necessary to transfer from each minibus a certain number
of passengers so that exactly d passengers
remain in each. This tranfer is only possible if a minibus initially contains
more than d passengers. For example, if
a > d, then a – d
passengers should be transferred from the first minibus. Similarly, b – d
passengers should be transferred from the second minibus (if b > d), and c – d passengers from the third minibus (if c > d).
Algorithm
realization
Read the input data.
scanf("%d %d %d",&a,&b,&c);
If the total number of people is not divisible by 3,
then print IMPOSSIBLE.
if((a + b + c) % 3 != 0)
puts("IMPOSSIBLE");
else
{
res = 0;
Compute the
number d of people in minibuses after the transfer.
d = (a + b + c) / 3;
In the variable res count the number of transfered people.
if (a > d) res += a - d;
if (b > d) res += b - d;
if (c > d) res += c - d;
Print the answer.
printf("%d\n",res);
}
Python realization
Read the input data.
a, b, c = map(int, input().split())
If the total number of people is not divisible by 3,
then print IMPOSSIBLE.
if (a + b + c) % 3 != 0:
print("IMPOSSIBLE")
else:
res = 0
Compute the
number d of people in minibuses after the transfer.
d = (a + b + c) // 3
In the variable res count the number of transfered people.
if a > d: res += a – d
if b > d: res += b – d
if c > d: res += c – d
Print the answer.
print(res)